/*
 * SupplierTableModel.java
 * 
 * Created: Aug 4, 2015
 * 
 * Copyright (c) ExpertPB 2015
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.stuff.supplier;

import com.emex.app.shared.data.Supplier;
import com.emex.app.stuff.gui.model.TableCrud;
import com.emex.app.stuff.provider.SupplierDataProvider;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey
 */
public class SupplierTableModel extends AbstractTableModel
        implements TableModel, TableCrud<Supplier> {

    /**
     * List of all orders.
     */
    private final List<Supplier> providers;
    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Название"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        String.class
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public SupplierTableModel() {
        providers = SupplierDataProvider.getInstance().getAllSuppliers();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return providers.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if ((rowIndex >= 0) && (rowIndex < providers.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return providers.get(rowIndex);
                }
                case 0: {
                    return providers.get(rowIndex).getName();
                }
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public void dataRemoved(Supplier t) {
        providers.remove(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataAdded(Supplier t) {
        providers.add(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataUpdated() {
        this.fireTableDataChanged();
    }
}
